package com.slkj.shilixiaoyuanapp.ui.parent.mine;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.UserEntity;
import com.slkj.shilixiaoyuanapp.entity.parent.StudentInfoEntity;
import com.slkj.shilixiaoyuanapp.net.CommonResult;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.BaseFunctionCallBack;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.util.StringUtils;
import com.slkj.shilixiaoyuanapp.util.UpdataFileUtil;
import com.slkj.shilixiaoyuanapp.util.picture.TakePictureUtils;
import com.slkj.shilixiaoyuanapp.view.DialogProvider;
import com.slkj.shilixiaoyuanapp.view.StateLayout;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import io.reactivex.ObservableSource;

@ActivityInfo(layout = R.layout.activity_student_info,title = "学生信息")
public class StudentInfoActivity extends BaseActivity {
    @InjectParam
    int stuId;
    @BindView(R.id.img_child_head)
    CircleImageView img_child_head;
    @BindView(R.id.tv_childname)
    TextView tv_childname;
    @BindView(R.id.tv_bj)
    TextView tv_bj;
    @BindView(R.id.tv_xh)
    TextView tv_xh;
    @BindView(R.id.tv_age)
    TextView tv_age;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.rl_img_child_head)
    RelativeLayout rl_img_child_head;
    public TakePictureUtils takePictureUtils;
    @BindView(R.id.statelayout)
    StateLayout statelayout;
    @BindView(R.id.img_choose)
    ImageView iv_sex;
    @Override
    protected void init() {
        StudentInfoActivity__JumpCenter.bind(this);
        getData();
        statelayout.setOnReLoadListener(this::getData);
    }

    private void getData() {
        HttpHeper.get().parentService().getStudentInfo(stuId)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<StudentInfoEntity>(statelayout) {
                    @Override
                    public void onCallBackSuccess(StudentInfoEntity data) {
                        Glide.with(StudentInfoActivity.this).load(data.getHeadImg()).asBitmap().centerCrop().into(img_child_head);
                        tv_childname.setText(data.getName());
                        tv_bj.setText(data.getClassName());
                        tv_xh.setText(data.getStudentCode());
                        tv_address.setText(data.getAddress());
                        tv_age.setText(data.getAge()+"");
                        if (data.isSex()){
                            Glide.with(StudentInfoActivity.this).load(R.drawable.boy).asBitmap().centerCrop().into(iv_sex);
                        }else {
                            Glide.with(StudentInfoActivity.this).load(R.drawable.girl).asBitmap().centerCrop().into(iv_sex);
                        }
                    }
                });
    }

    /**
     * 修改头像
     */
    @OnClick(R.id.rl_img_child_head)
    void changeHead() {
        takePictureUtils = TakePictureUtils.Builder.from(this).isCompressor(false).isTailor(false).creat();
        takePictureUtils.setTakeCallBacklistener(new TakePictureUtils.TakePictureListener() {
            @Override
            public void successful(File outFile) {

                upNewHead(outFile.getPath());
            }

            @Override
            public void failed(int errorCode, List<String> deniedPermissions) {
            }
        });
        showPhotoDialog();
    }

    private void showPhotoDialog() {
        DialogProvider.getPhotoDialog(this, new DialogProvider.TakePicTypeListener() {
            @Override
            public void takePhoto() {
                takePictureUtils.getByCarema();
            }
            @Override
            public void chosePhoto() {
                takePictureUtils.getByAlbum();
            }
        });
    }

    /**
     * 上传新头像
     *
     * @param path
     */
    private void upNewHead(String path) {
        List<String> picPaths = new ArrayList<>();
        picPaths.add(path);
        UpdataFileUtil.UpImgObservable("head", picPaths)
                .flatMap(new BaseFunctionCallBack<List<String>, String>() {
                    @Override
                    public ObservableSource<CommonResult<String>> back(List<String> result) {
                        return HttpHeper.get().parentService().updateHeadImg(StringUtils.listToString(result),stuId);
                    }
                })
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<String>(true,StudentInfoActivity.this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        Glide.with(StudentInfoActivity.this).load(data).into(img_child_head);
                        LoadSuccessAndFailDialog.showSuccess(StudentInfoActivity.this,"上传成功");
                        UserEntity userEntity = UserRequest.getInstance().getUser();
                        for (int i=0;i<UserRequest.getInstance().getUser().getStudents().size();i++){
                            if (UserRequest.getInstance().getUser().getStudents().get(i).getIsSelect()==1){
                                userEntity.getStudents().get(i).setHead(data);
                            }
                        }
                        UserRequest.getInstance().setUser(userEntity);
                    }
                });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        takePictureUtils.attachToActivityForResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        takePictureUtils.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
